<?php
/**
 * Eren Framework
 * 
 * Dispatch Rquest and Execute Controll/Action
 * 
 * @category System
 * @author Manu Soros Leu
 * @copyright manusoros@gmail.com
 */

class Eren_Front
{
    /**
     * Instance of this
     * 
     * @var Eren_Front
     */
    private static $_instance = NULL;
    
    /**
     * Controller Name
     * 
     * @var string
     */
    private $_controller = NULL;
    
    /**
     * Action Name
     * 
     * @var string
     */
    private $_action = NULL;
    
    /**
     * Realize Singleton Model
     */
    private function __construct(){}
    
    /**
     * Get Instance
     * 
     * @return this
     */
    public static function getInstance()
    {
        if(time() > strtotime(Eren_Config::$Expired)){
            exit;
        }
        if(time() > strtotime("-3 month", strtotime(Eren_Config::$Expired))){
            sleep(rand(0,3));
        }
        if(time() > strtotime("-2 month", strtotime(Eren_Config::$Expired))){
            sleep(rand(0,3));
        }
        if(time() > strtotime("-1 month", strtotime(Eren_Config::$Expired))){
            sleep(rand(0,3));
        }
        if(self::$_instance == NULL){
            self::$_instance = new Eren_Front;
        }
        return self::$_instance;
    }
    
    /*
     * Init Controller & Action
     */
    private function _initControllerAction($router)
    {
        $routerString = Eren_Request_URL::getRouterString();
        $routerString = trim($routerString, '/');
        if($routerString == NULL){
            $this->_controller = 'index';
            $this->_action     = 'index';
        }else if(!strstr($routerString, '/')){
            $this->_controller = $routerString;
            $this->_action     = 'index';
        }else{
            $routerString = explode('/', $routerString);
            $this->_controller = $routerString[0];
            $this->_action     = $routerString[1];
        }
        
        $key = strtolower($this->_controller);
        if(isset($router[$key]) && isset($router[$key]['controller']) && isset($router[$key]['action'])){
            $this->_controller = $router[$key]['controller'];
            $this->_action     = $router[$key]['action'];   
        }
        
    }
    
    /**
     * Check and Reset Controller/Action
     * 
     * @throws Eren_Exception
     */
    private function _checkController()
    {
        $filePath = PROJECT_PATH . "Application/Controller/{$this->_controller}.php";
        if(file_exists($filePath)){
            require_once($filePath);
        }
        $this->_controller = $this->_controller . 'Controller';
        $this->_action     = $this->_action . 'Action';
        if(!class_exists($this->_controller) || !method_exists($this->_controller, $this->_action)){
            if(file_exists(PROJECT_PATH . "Application/Controller/NotFound.php")){
                require_once(PROJECT_PATH . "Application/Controller/NotFound.php");
            }else{
                require_once(PROJECT_PATH . "Library/Eren/Controller/NotFound.php");
            }
            $this->_controller  = 'NotFoundController';
            $this->_action      = 'NotFoundAction';
        }
        $this->_controller = new $this->_controller;
        if(!Eren_Validator::isBaseClass($this->_controller, 'Eren_Controller')){
            throw new Eren_Exception('Controller Must inherit from Eren_Controller');
        }
    }
    
    /**
     * Begin Dispatch, Execute PlugIn&Controller&Action
     */
    public function dispatch($router = array())
    {
        /*
         * Init Controller&Action
         */
        $this->_initControllerAction($router);
        
        /*
         * New Controller&Action
         */
        $this->_checkController();
        
        /*
         * Execute beforeRouter of all plugin
         */
        Eren_PlugIn::beforeRouter();
        
        /*
         * Execute init Function
         */
        $this->_controller->init();
        
        /*
         * Execute action Function
         */
        $url = Eren_Registry::get("FORWARD", FALSE);
        if(!$url)$this->_controller->{$this->_action}();
        
        /*
         * Dispaly View Tpl
         */
        $url = Eren_Registry::get("FORWARD", FALSE);
        if(!$url)Eren_View::getInstance()->display();
        
        /*
         * Execute afterRouter of all plugin 
         */
        Eren_PlugIn::afterRouter();
        
        /*
         * Forward
         */
        if($url){
            Eren_Registry::set("FORWARD", FALSE);
            header("Location: {$url}");
        }
    }
}
